package logic

import (
	"context"
	"encoding/json"
	"github.com/olivere/elastic/v7"
	"server/models"
	"server/proto"
)

// todo: 推荐
func (s *ServerRpc) EsCount(ctx context.Context, in *proto.EsCountReq) (*proto.EsCountRes, error) {
	agg := elastic.NewTermsAggregation().Field(in.Title + ".keyword").Size(10).OrderByCountDesc()
	res, err := models.EsCount(in.Index, agg, in.Title)
	if err != nil {
		return &proto.EsCountRes{
			Data: nil,
		}, err
	}
	mar, _ := json.Marshal(res)
	return &proto.EsCountRes{
		Data: mar,
	}, nil
}
